/*
 * @Author: song
 * @Description:timeline 标题
 * @Date: 2024年04月28日 星期日 20:04:31
 * @Modify:
 */

using System;
using Function.SkillEditor.Runtime;
using SCore.Editor.WindowDraw;
using SCore.Function.Fixed;
using UnityEditor;
using UnityEngine;

namespace Function.SkillEditor.Editor
{
    public class SkillTimeLineHeadView : NCommonView
    {
        private bool play;

        [Tooltip("播放状态")]
        public bool _play
        {
            get { return play; }
            set
            {
                play = value;
                if (play)
                {
                    if (_cursorView._curFrame >= GetSubEditorWindow<SkillTimeLineSubWindow>()._skillConfigItem
                            ._skillRunLine
                            ._lengthFrame)
                    {
                        _cursorView._curFrame = 0;
                    }
                }

                SkillEditorWindow.EventManager.BroadCast(SkillEditorEventType.TimeLinePlayStatusChange, play);
            }
        }

        public override void OnEnable()
        {
            SkillEditorWindow.EventManager.AddListener<SkillConfigItemData>(SkillEditorEventType.TimeLineEditChange,
                OnTimeLineEditChange);
        }

        public override void OnDisable()
        {
            SkillEditorWindow.EventManager.RemoveListener<SkillConfigItemData>(SkillEditorEventType.TimeLineEditChange,
                OnTimeLineEditChange);
        }


        private SkillTimeLineCursorView _cursorView;
        private DateTime _lastTime;
        private fp _deltaTime;

        public void OnHeadAGUI()
        {
            if (GUILayout.Button(EditorContentHelp.GotoBeginingContent, EditorStyles.toolbarButton))
            {
                _cursorView._curFrame = 0;
            }

            if (GUILayout.Button(EditorContentHelp.PreviousFrameContent, EditorStyles.toolbarButton))
            {
                if (_cursorView._curFrame > 0)
                {
                    _cursorView._curFrame -= 1;
                }
            }

            EditorGUI.BeginChangeCheck();
            var isPlaying = GUILayout.Toggle(_play, EditorContentHelp.PlayContent,
                EditorStyles.toolbarButton);
            if (EditorGUI.EndChangeCheck())
            {
                _play = isPlaying;
            }

            if (GUILayout.Button(EditorContentHelp.NextFrameContent, EditorStyles.toolbarButton))
            {
                _cursorView._curFrame += 1;
            }

            if (GUILayout.Button(EditorContentHelp.GotoEndContent, EditorStyles.toolbarButton))
            {
                var timelineWindow = GetSubEditorWindow<SkillTimeLineSubWindow>();
                _cursorView._curFrame = timelineWindow._skillConfigItem._skillRunLine._lengthFrame;
            }

            EditorGUI.BeginChangeCheck();
            var currentTime = _cursorView._curFrame;
            var r = EditorGUILayout.GetControlRect(false, 18f, EditorStyles.toolbarTextField,
                GUILayout.Width(50f));
            var newCurrentTime =
                EditorGUI.TextField(r, GUIContent.none, currentTime.ToString(),
                    EditorStyles.toolbarTextField);
            if (EditorGUI.EndChangeCheck())
            {
                _cursorView._curFrame = int.Parse(newCurrentTime);
            }
        }

        public void OnHeadBGUI()
        {
            var timelineWindow = GetSubEditorWindow<SkillTimeLineSubWindow>();
            timelineWindow._skillConfigItem._skillRunLine =
                EditorGUILayout.ObjectField(timelineWindow._skillConfigItem._skillRunLine, typeof(SkillRunLine)) as
                    SkillRunLine;
            timelineWindow._skillConfigItem._editorGo =
                EditorGUILayout.ObjectField(timelineWindow._skillConfigItem._editorGo,
                    typeof(GameObject)) as GameObject;
        }

        public void OnHeadGUI()
        {
            using (new GUILayout.HorizontalScope())
            {
                OnHeadAGUI();
            }

            GUILayout.FlexibleSpace();
            using (new GUILayout.HorizontalScope())
            {
                OnHeadBGUI();
            }
        }

        public void Bind(SkillTimeLineCursorView cursorView)
        {
            _cursorView = cursorView;
        }

        public override void Update()
        {
            DateTime now = DateTime.Now;
            _deltaTime = (now - _lastTime).TotalSeconds;
            _lastTime = now;
            if (_play)
            {
                if (_cursorView._curFrame > GetSubEditorWindow<SkillTimeLineSubWindow>()._skillConfigItem._skillRunLine
                        ._lengthFrame)
                {
                    _play = false;
                    _cursorView._curFrame = fp.Zero;
                }
                else
                {
                    //下一帧判断 是否需要停止
                    _cursorView._curFrame +=
                        _deltaTime / (fp)SkillRunMap.Instance.GetEOneFrameTime();
                }
            }
        }

        private void OnTimeLineEditChange(SkillConfigItemData itemData)
        {
            _play = false;
        }
    }
}